package com.ruoyi.service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.entity.BizBusinessSync;
import com.ruoyi.mapper.BizBusinessSyncMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.BizBusinessSyncFrom;
import com.sxftech.resp.ResponseData;
/**
 * <p>
 * 业务数据同步表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-20
 */
@Service
@DataSource(value = DataSourceType.SLAVE)  //切换数据源
public class BizBusinessSyncService extends ServiceImpl<BizBusinessSyncMapper, BizBusinessSync> {

	public ResponseData create(BizBusinessSyncFrom bizBusinessSync) {
		BizBusinessSync entity = transformationFrom(bizBusinessSync);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(BizBusinessSyncFrom bizBusinessSync) {
		if(findById(bizBusinessSync.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizBusinessSync entity = transformationFrom(bizBusinessSync);
		entity.setId(bizBusinessSync.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public BizBusinessSync transformationFrom(BizBusinessSyncFrom bizBusinessSync) {
		BizBusinessSync entity = new BizBusinessSync();
		entity.setBizId(bizBusinessSync.getBizId());
		entity.setOperType(bizBusinessSync.getOperType());
		entity.setPlCode(bizBusinessSync.getPlCode());
		entity.setSpaceCode(bizBusinessSync.getSpaceCode());
		entity.setValidateTime(bizBusinessSync.getValidateTime());
		entity.setPhone(bizBusinessSync.getPhone());
		entity.setSpaceType(bizBusinessSync.getSpaceType());
		entity.setName(bizBusinessSync.getName());
		entity.setPlateNumber(bizBusinessSync.getPlateNumber());
		entity.setPrice(bizBusinessSync.getPrice());
		entity.setCarType(bizBusinessSync.getCarType());
		entity.setOperStatus(bizBusinessSync.getOperStatus());
		entity.setInsideZoneCode(bizBusinessSync.getInsideZoneCode());
		entity.setRemark(bizBusinessSync.getRemark());
		entity.setCreator(bizBusinessSync.getCreator());
		entity.setCreateTime(bizBusinessSync.getCreateTime());
		entity.setModifier(bizBusinessSync.getModifier());
		entity.setModifyTime(bizBusinessSync.getModifyTime());
		return entity;
	}

	public BizBusinessSyncFrom transformationBean(BizBusinessSync bizBusinessSync) {
		BizBusinessSyncFrom from = new BizBusinessSyncFrom();
		from.setId(bizBusinessSync.getId());
		from.setBizId(bizBusinessSync.getBizId());
		from.setOperType(bizBusinessSync.getOperType());
		from.setPlCode(bizBusinessSync.getPlCode());
		from.setSpaceCode(bizBusinessSync.getSpaceCode());
		from.setValidateTime(bizBusinessSync.getValidateTime());
		from.setPhone(bizBusinessSync.getPhone());
		from.setSpaceType(bizBusinessSync.getSpaceType());
		from.setName(bizBusinessSync.getName());
		from.setPlateNumber(bizBusinessSync.getPlateNumber());
		from.setPrice(bizBusinessSync.getPrice());
		from.setCarType(bizBusinessSync.getCarType());
		from.setOperStatus(bizBusinessSync.getOperStatus());
		from.setInsideZoneCode(bizBusinessSync.getInsideZoneCode());
		from.setRemark(bizBusinessSync.getRemark());
		from.setCreator(bizBusinessSync.getCreator());
		from.setCreateTime(bizBusinessSync.getCreateTime());
		from.setModifier(bizBusinessSync.getModifier());
		from.setModifyTime(bizBusinessSync.getModifyTime());

		return from;
	}

	
	public BizBusinessSyncFrom findById(String id) {
		BizBusinessSync entity = this.getOne(new QueryWrapper<BizBusinessSync>().lambda()
				.eq(BizBusinessSync::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizBusinessSync bizBusinessSync = new BizBusinessSync();
		bizBusinessSync.setId(id);
		this.updateById(bizBusinessSync);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, BizBusinessSyncFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<BizBusinessSync>().lambda()
				.eq(ibean.getBizId() != null, BizBusinessSync::getBizId, ibean.getBizId())
				.eq(StringUtils.isNotEmpty(ibean.getOperType()), BizBusinessSync::getOperType, ibean.getOperType())
				.eq(StringUtils.isNotEmpty(ibean.getPlCode()), BizBusinessSync::getPlCode, ibean.getPlCode())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceCode()), BizBusinessSync::getSpaceCode, ibean.getSpaceCode())
				.eq(StringUtils.isNotEmpty(ibean.getValidateTime()), BizBusinessSync::getValidateTime, ibean.getValidateTime())
				.eq(StringUtils.isNotEmpty(ibean.getPhone()), BizBusinessSync::getPhone, ibean.getPhone())
				.eq(StringUtils.isNotEmpty(ibean.getSpaceType()), BizBusinessSync::getSpaceType, ibean.getSpaceType())
				.eq(StringUtils.isNotEmpty(ibean.getName()), BizBusinessSync::getName, ibean.getName())
				.eq(StringUtils.isNotEmpty(ibean.getPlateNumber()), BizBusinessSync::getPlateNumber, ibean.getPlateNumber())
				.eq(ibean.getPrice() != null, BizBusinessSync::getPrice, ibean.getPrice())
				.eq(StringUtils.isNotEmpty(ibean.getCarType()), BizBusinessSync::getCarType, ibean.getCarType())
				.eq(ibean.getOperStatus() != null, BizBusinessSync::getOperStatus, ibean.getOperStatus())
				.eq(StringUtils.isNotEmpty(ibean.getInsideZoneCode()), BizBusinessSync::getInsideZoneCode, ibean.getInsideZoneCode())
				.eq(StringUtils.isNotEmpty(ibean.getRemark()), BizBusinessSync::getRemark, ibean.getRemark())
				.eq(ibean.getCreator() != null, BizBusinessSync::getCreator, ibean.getCreator())
				.eq(ibean.getCreateTime() != null, BizBusinessSync::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifier() != null, BizBusinessSync::getModifier, ibean.getModifier())
				.eq(ibean.getModifyTime() != null, BizBusinessSync::getModifyTime, ibean.getModifyTime())
				);
	}
}
